package com.aloda.orangebank.service.impl;

import com.aliyuncs.ram.model.v20150501.ListEntitiesForPolicyResponse;
import com.aloda.orangebank.mapper.SysAdminMapper;
import com.aloda.orangebank.mapper.UserMapper;
import com.aloda.orangebank.pojo.Admin;
import com.aloda.orangebank.pojo.PageBean;
import com.aloda.orangebank.pojo.User;
import com.aloda.orangebank.service.SysAdminService;
import com.aloda.orangebank.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Random;

@Service
public class SysAdminServiceImpl implements SysAdminService {

    @Autowired
    private SysAdminMapper sysAdminMapper;

    @Override
    public int getTotals() {
        return sysAdminMapper.getTotals();
    }

    @Override
    public void deleteUser(String username) {
        sysAdminMapper.deleteCanceledUserByUsername(username);
    }

    @Override
    public void cancelUser(String username) {
        User user = sysAdminMapper.getUserByUsername(username);//项目优化时考虑合并为一个统一事务
        user.setUpdateTime(LocalDateTime.now());
        sysAdminMapper.deleteUserByUsername(username);
        sysAdminMapper.addCancelledUser(user);
    }

    @Override
    public PageBean pageCancel(Integer page, Integer pageSize, String username, LocalDateTime begin, LocalDateTime end) {
        //1.设置分页参数
        PageHelper.startPage(page,pageSize);
        //2。执行查询
        List<User> userList = sysAdminMapper.listCancel(username, begin, end);
        Page<User> p = (Page<User>)userList;
        //3.封装PageBean对象
        PageBean pageBean= new PageBean(p.getTotal(),p.getResult());

        return pageBean;
    }

    @Override
    public PageBean page(Integer page, Integer pageSize, String username, LocalDateTime begin, LocalDateTime end) {
        //1.设置分页参数
        PageHelper.startPage(page,pageSize);
        //2。执行查询
        List<User> userList = sysAdminMapper.list(username, begin, end);
        Page<User> p = (Page<User>)userList;
        //3.封装PageBean对象
        PageBean pageBean= new PageBean(p.getTotal(),p.getResult());

        return pageBean;
    }

    @Override
    public List<User> normalTable() {
        return sysAdminMapper.normalTable();
    }

    @Override
    public void recoverUser(Integer id) {
        User u = sysAdminMapper.getCanceledUserById(id);//考虑设为事务
//        System.out.println(u);
        do {
            // 生成随机数字 为恢复的用户生成一个随机的用户名
            Random random = new Random();
            int randomNumber = random.nextInt(1000000);

            // 将数字转换为字符串
            String randomString = String.format("%06d", randomNumber);

            // 在前面添加字母 "n"
           u.setUsername("n"+randomString);
        }while (sysAdminMapper.getUserByUsername(u.getUsername())!=null);
        u.setUpdateTime(LocalDateTime.now());
        sysAdminMapper.addRecoverUser(u);
        sysAdminMapper.deleteCanceledUserById(u.getId());
    }

    @Override
    public Admin login(Admin admin) {
        Admin a = sysAdminMapper.login(admin);
        if(a!=null){
            a.setLoginTime(LocalDateTime.now());//下版本需要考虑组合为事物
            sysAdminMapper.updateLoginTime(a);
            return a;
        }
        return null;
    }
}
